package noahzu.github.io.baselib.utils.fragmentMaker;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import noahzu.github.io.baselib.R;
import noahzu.github.io.baselib.base.BaseActivity;
import noahzu.github.io.baselib.baseWidget.HeaderView;


/**
 * Created by gejinpeng on 2017/9/28.
 */

public class FragmentTransferActivity extends BaseActivity {
    private FragmentSpec mSpec;
    private HeaderView mHeaderView;

    @Override
    protected int getLayout() {
        mSpec = FragmentSpec.getInstance();
        return R.layout.activity_fragment_only;
    }

    @Override
    protected void findView() {
        mHeaderView = (HeaderView) findViewById(R.id.header_view);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void initView() {
        onUIReady();
    }


    protected void onUIReady() {
        try {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            Fragment fragment = mSpec.fragment;
            if (fragment == null) {
                fragment = Fragment.instantiate(this, mSpec.fragmentClass.getName(), mSpec.arguments);
            }
            if (mSpec.arguments != null) {
                fragment.setArguments(mSpec.arguments);
            }
            transaction.replace(R.id.content_layout, fragment);
            transaction.commitAllowingStateLoss();
        } catch (Exception e) {
            throw new IllegalArgumentException("you should pass a valid fragment class!");
        }



        mHeaderView.setVisibility(mSpec.mIsShowTitleView?View.VISIBLE:View.GONE);

        if (mSpec.mIsShowTitleView && mSpec.mIsTitleViewBackFinish){
            mHeaderView.setOnBackListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            mHeaderView.setTitle(mSpec.mTitle);
        }
    }

}
